<!--
 * @Author: 清酒吖
 * @Date: 2022-01-19 10:59:05
 * @Email:  
 * @LastEditors:  
 * @LastEditTime: 2022-01-21 17:17:02
 * @Description: 
 * @version: x 1.0
-->

<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <i class="el-icon-search" />
        <span>筛选搜素</span>
        <el-button
          style="float: right"
          type="info"
          round
          @click="handleResetSearch()"
          >重置</el-button
        >
        <el-button
          style="float: right; margin-right: 15px"
          type="primary"
          round
          @click="search()"
          >查询</el-button
        >
        <el-button
          style="float: right; margin-right: 15px"
          type="danger"
          round
          @click="removeBath()"
          >批量删除</el-button
        >
      </div>
      <div class="text item">
        <el-form :inline="true" :model="spec" class="demo-form-inline">
          <el-form-item label="输入搜索:">
            <el-input
              v-model="spec.goodsSpec.specName"
              placeholder="规格名称"
              clearable
              @clear="search()"
            ></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-input
              v-model="spec.optionName"
              placeholder="规格选项名称"
              clearable
              @clear="search()"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <!-- <el-button type="primary" @click="onSubmit">查询</el-button> -->
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <!-- 添加 -->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表 </span>
      <el-button
        type="success"
        size="mini"
        icon="el-icon-plus"
        @click="dialogFormVisible = true"
        >添加</el-button
      >
    </el-card>

    <!-- 添加规格 -->
    <el-dialog
      title="规格编辑"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
    >
      <el-form>
        <el-form-item label="规格名称" label-width="100px">
          <el-input v-model.trim="entity.specName"></el-input>
        </el-form-item>

        <el-button type="success" icon="el-icon-plus" @click="addOptions()"
          >添加规格选项</el-button
        >

        <el-table :data="entity.options" style="width: 100%">
          <el-table-column prop="date" label="规格选项名称" align="center">
            <template slot-scope="scope">
              <el-input
                v-model.trim="entity.options[scope.$index].optionName"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                type="primary"
                @click="delOpt(scope.$index)"
                icon="el-icon-delete"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          @click="
            dialogFormVisible = false;
            resetSpec();
          "
          >取 消</el-button
        >
        <el-button type="primary" @click="saveSpec()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 表格数据  -->
    <el-table
      ref="multipleTable"
      :data="specList"
      tooltip-effect="dark"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      ></el-table-column>

      <el-table-column prop="id" label="规格选项编号" align="center">
      </el-table-column>
      <el-table-column prop="optionName" label="规格选项名称" align="center">
      </el-table-column>
      <el-table-column prop="goodsSpec.id" label="所属规格编号" align="center">
      </el-table-column>
      <el-table-column
        prop="goodsSpec.specName"
        label="所属规格名称"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="" label="操作" align="center">
        <template slot-scope="scope">
          <div>
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="toUpdate(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="remove(scope.row)"
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="float: right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 修改会话框 -->
    <!-- 表单 -->
    <template>
      <el-dialog
        title="修改商品规格选项信息"
        :visible.sync="dialogUpdateFormVisible"
      >
        <el-form
          :model="updateEntity"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="规格名称">
            <el-input
              v-model="updateEntity.goodsSpec.specName"
              :disabled="true"
            ></el-input>
          </el-form-item>

          <el-form-item label="规格选项名称">
            <el-input v-model="updateEntity.optionName"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogUpdateFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateSpecOptions()"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </template>
  </div>
</template>

<script>
import {
  findSpecOptions,
  addSpecAndOptions,
  updateOptions,
  removeSpecOption,
  removeBrathSpecAndOptions,
} from "@/api/goodsSpec";

export default {
  data() {
    return {
      pageNum: 1, // * 当前页数
      pageSize: 5, // * 当前页记录数
      total: 0, // * 总记录数
      pages: 0, // * 总页数
      spec: {
        // * 查询条件
        optionName: "",
        goodsSpec: {
          specName: "",
        },
      },
      specList: [], // * 页面渲染数据
      multipleSelection: [], // * 表格多选数据
      dialogFormVisible: false, // * 添加表单框显示状态
      dialogUpdateFormVisible: false, // * 修改表单框显示状态
      entity: {
        // -> 表单选项
        specName: "",
        options: [
          {
            optionName: "",
          },
        ],
      },
      updateEntity: {
        // -> 修改表单数据
        optionName: "",
        goodsSpec: {
          specName: "",
        },
      },
      specOptionVO: {
        // 批量删除参数
        optionsId: [],
        specId: [],
      },
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // * 每页显示记录数
    handleSizeChange(pageSizeNow) {
      // -> 监听当前页显示条数的变化
      console.log(pageSizeNow);
      this.pageSize = pageSizeNow;
      this.search();
    },
    // * 分页
    handleCurrentChange(pageNumNow) {
      //-> 监听当前页的变化
      console.log(pageNumNow);
      this.pageNum = pageNumNow;
      this.search();
    },
    // -> 查询产品规格和选项
    search() {
      // -> 与服务器交互,调用查询接口
      findSpecOptions(this.spec, this.pageNum, this.pageSize).then((res) => {
        // * 查询结果集
        this.specList = res.pageList;
        // * 查询的所有结果数
        this.total = res.total;
        // * 总页数
        this.pages = res.pages;
      });
    },
    // -> 重置查询条件
    handleResetSearch() {
      (this.spec = {
        // * 查询条件
        optionName: "",
        goodsSpec: {
          specName: "",
        },
      }),
        this.search();
    },
    addOptions() {
      // -> 添加规格选项
      this.entity.options.push({});
    },
    delOpt(index) {
      // -> 删除规格选项
      this.entity.options.splice(index, 1);
    },
    resetSpec() {
      // -> 重置表单

      this.entity = {
        specName : "",
        // -> 表单选项
        options: [
          {
            optionName: "",
          },
        ],
      };
    },
    saveSpec() {
      // -> 添加规格和选项
      // -> 与服务器交互，调用添加规格和规格选项的API
      addSpecAndOptions(this.entity).then((res) => {
        if (res.success) {
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
            duration: 3000,
          });
          this.pageNum = 1;
          this.search();
        } else {
          this.$notify.error({
            title: "失败",
            message: res.msg,
            type: "success",
            duration: 3000,
          });
        }
      });
      this.dialogFormVisible = false;
      this.entity = {
        // -> 表单选项
        options: [
          {
            optionName: "",
          },
        ],
      };
    },
    // -> 打开修改会话框
    toUpdate(row) {
      this.dialogUpdateFormVisible = true;
      this.updateEntity = JSON.parse(JSON.stringify(row));
    },
    // -> 修改操作
    updateSpecOptions() {
      // *与服务器交互，调用修改API
      updateOptions(this.updateEntity).then((res) => {
        if (res.success) {
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
            duration: 3000,
          });
          this.pageNum = 1;
          this.search();
        } else {
          this.$notify.error({
            title: "失败",
            message: res.msg,
            type: "success",
            duration: 3000,
          });
        }
      });
      this.dialogUpdateFormVisible = false;
    },
    // -> 删除商品规格
    remove(row) {
      // * 与服务器交互，调用删除接口
      removeSpecOption(row).then((res) => {
        if (res.success) {
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
            duration: 3000,
          });
          this.pageNum = 1;
          this.search();
        } else {
          this.$notify.error({
            title: "失败",
            message: res.msg,
            type: "success",
            duration: 3000,
          });
        }
      });
    },
    // -> 批量删除
    removeBath() {
      this.specOptionVO = {
        specId: [],
        optionsId: [],
      };

      this.multipleSelection.forEach((item) => {
        this.specOptionVO.specId.push(item.specId);
        this.specOptionVO.optionsId.push(item.id);
      });

            this.$confirm("此操作将永久删除该商品品牌, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // -> 与服务器交互，调用删除方法接口
          removeBrathSpecAndOptions(this.specOptionVO).then((res) => {
            if (res.success) {
              this.$notify({
                title: "成功",
                message: res.msg,
                type: "success",
                duration: 3000,
              });
              this.pageNum = 1;
              this.search();
            } else {
              this.$notify.error({
                title: "失败",
                message: res.msg,
                type: "success",
                duration: 3000,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.search();
  },
};
</script>